package com.yc.backstage.controller;

import com.yc.backstage.service.ExcelService;
import com.yc.common.enums.BusinessType;
import com.yc.common.res.ResultClass;
import com.yc.logging.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Classname ExcelController
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/8/29 20:48
 * @Created by Mikechen
 */
@RestController
@RequestMapping("system/easyExcel")
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    /**
     * 导入用户数据
     * @return
     */
    @PreAuthorize("@el.hasPermi('system:user:update')")
    @PostMapping("/importUser")
    public ResultClass importUser(@RequestParam("file") MultipartFile file) throws IOException {
        excelService.importData(file);
        return ResultClass.success();
    }

    /**
     * 导出用户数据
     * @return
     */
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@el.hasPermi('system:user:query')")
    @PostMapping("/exportUser")
    public ResultClass exportUser(HttpServletResponse response){
        try {
            return ResultClass.success(excelService.exportUser(response));
        }catch (Exception e){
            return ResultClass.fail("导出失败");
        }
    }

}
